﻿// Copyright (c) 2014 Jonathan Magnan (http://jonathanmagnan.com/).
// All rights reserved (http://zzzproject.com/sqlbulkcopy-extensions/).
// Licensed under MIT License (MIT) (https://zsqlbulkcopy.codeplex.com/license/).
using System.Collections.Generic;
#if NET35 || NET40
using System.Linq;

#elif NET20
using System.Collections.Generic;
using System.Data.SqlClient;
using Z.Net.Legacy20;
#endif
using System.Linq;

namespace Z.Net.Extensions
{
    internal partial class SqlBulkOperation
    {
        /// <summary>
        ///     Sets SQL bulk update action.
        /// </summary>
        public void SetSqlInsertActionSingle()
        { 
            IEnumerable<string> updateColumnSelectList = MappingList.Where(x => x.DestinationColumn != Setting.IdentityColumn)
                .Select(x => string.Format("[{0}]", x.DestinationColumn));

            IEnumerable<string> updateColumnInsertList = MappingList.Where(x => x.DestinationColumn != Setting.IdentityColumn)
                .Select(x => string.Format("@{0}", ParameterMapping[x.DestinationColumn]));

            string sqlSetValue = string.Join(", ", updateColumnSelectList.ToArray());
            string sqlInsertValue = string.Join(", ", updateColumnInsertList.ToArray());
 
//            IEnumerable<SqlBulkCopyColumnMapping> mappingListWhere = IEnumerableExtensions.Where(MappingList, x => x.DestinationColumn != Setting.IdentityColumn);

//            IEnumerable<string> updateColumnSelect = IEnumerableExtensions.SelectString(mappingListWhere, x => x.Item2 = string.Format("[{0}]", x.Item1.DestinationColumn));
//            IEnumerable<string> updateColumnInsert = IEnumerableExtensions.SelectString(mappingListWhere, x => x.Item2 = string.Format("@{0}", ParameterMapping[x.Item1.DestinationColumn]));

//            var updateColumnSelectList = new List<string>(updateColumnSelect);
//            var updateColumnInsertList = new List<string>(updateColumnInsert);

//            string sqlSetValue = string.Join(", ", updateColumnSelectList.ToArray());
//            string sqlInsertValue = string.Join(", ", updateColumnInsertList.ToArray());
//#endif


            SqlActionBulk1 = string.Format(SqlInsertActionSingle, SqlBulkCopy.DestinationTableName, TemporaryTableName, sqlSetValue, sqlInsertValue);
        }
    }
}